declare namespace g = "ddi:group:3_1";
declare namespace dc="ddi:dcelements:3_1";
declare namespace d="ddi:datacollection:3_1"; 
declare namespace dc2="http://purl.org/dc/elements/1.1/"; 
declare namespace s="ddi:studyunit:3_1"; 
declare namespace c="ddi:conceptualcomponent:3_1";
declare namespace r="ddi:reusable:3_1"; 
declare namespace a="ddi:archive:3_1"; 
declare namespace ddi="ddi:instance:3_1"; 
declare namespace l="ddi:logicalproduct:3_1";

import module namespace request="http://exist-db.org/xquery/request";
import module namespace json="http://www.json.org";

declare option exist:serialize "method=json media-type=text/javascript";

(: if callback is defined :)
let $callback := request:get-parameter("callback", ())

let $qid := <query><phrase>{request:get-parameter("qid", ())}</phrase></query>

(:
let $studies := collection('/db/ddi/data/ddi3/internal')/ddi:DDIInstance/s:StudyUnit[.//d:MultipleQuestionItem/r:UserID=xs:integer($qid) or .//d:QuestionItem/r:UserID=xs:integer($qid)]
:)

let $studies := collection('/db/ddi/data/ddi3/internal')/ddi:DDIInstance/s:StudyUnit[.//d:MultipleQuestionItem[ft:query(r:UserID, $qid)] or .//d:QuestionItem[ft:query(r:UserID, $qid)]]


let $output := <doc>
                    <question-id>{$qid}</question-id>
                    {
                    for $s in $studies
			order by $s/r:Citation/r:Title[@lang='sv']
                        return 
                        <study>
                           <questionItemName>
                                {
                                   if(count($s//d:QuestionItem[r:UserID=$qid]/d:QuestionItemName) > 0)
                                    then
                                    for $qi in $s//d:QuestionItem[r:UserID=$qid]/d:QuestionItemName
                                        return element {$qi/@xml:lang} {fn:string($qi)}
                                    else
                                    for $qi in $s//d:MultipleQuestionItem[r:UserID=$qid]/d:MultipleQuestionItemName
                                        return element {$qi/@xml:lang} {fn:string($qi)}                                       
                                 }
                           </questionItemName>
                                
                           <studyId>{fn:string($s/@id)}</studyId>
                           <study_id>{fn:string($s/r:UserID[@type='study_id'])}</study_id>
			   			   <title>{                    
                                    for $t in $s/r:Citation/r:Title
                                        return element {$t/@xml:lang} {fn:string($t)}
                           }</title>
                           <series>
                            <series_id>{fn:string($s/r:UserID[@type='series_id'])}</series_id>
			   	               <name>{                    
                                    for $t in $s/r:SeriesStatement/r:SeriesName
                                        return element {$t/@xml:lang} {fn:string($t)}
                               }</name>
                           </series>
                        </study>
                    }
               </doc>

return $output